<?php

namespace app\api\controller;

use app\admin\model\SystemBanner;
use app\admin\model\SystemCate;
use app\admin\model\SystemGame;
use app\admin\model\SystemPlatform;
use app\api\model\Comment;
use app\api\model\Follow;
use app\api\model\PictureCollection;
use app\api\model\PictureLike;
use app\api\model\ZhaopinModel;
use app\common\controller\ApiController;
use think\App;
use app\api\model\Picture;
use GuzzleHttp\Psr7\Message;
use think\facade\Db;
use think\facade\Filesystem;

class Zhaopin extends ApiController
{

    /**
     * 画列表
     * @return string
     * @throws \Exception
     */

    public function index(){


        return $this->fetch();
    }
    public function upload()
    {
        //获取表单的上传数据
        $file = request()->file('image');
        //目录在 runtime/storage/toppic/时间/文件
        $savename = Filesystem::disk('public')->putFile('topic', $file);
        if ($savename) {
            $this->success('上传成功', ['url' => '/'.str_replace('\\','/',$savename)]);
        } else {

            $this->error($file->getError());
        }
    }
    public function save(){
        Db::startTrans();
        try {
            $list = request()->param('list');
        $photo = request()->param('photo');
        $glx = request()->param('glx');   
        $job = request()->param('job');
        $hr = request()->param('hr');
        $hr_from = request()->param('hr_from');
        $pin_from = request()->param('pin_from');
        $data['glx'] = $glx;
        $data['job'] = $job;
        $data['hr'] = $hr;
        $data['hr_from'] = $hr_from;
        $data['pin_from'] = $pin_from;

        foreach($list as $k=>$v){
            if(isset($v['value'])){
                $data[$v['field']] = $v['value'];
            }
            if(isset($v['date'])){
                if($v['field'] == 'birthday'){
                    $data[$v['field']] .= ' '.$v['date']; 
                }else {
                    $data[$v['field']] = $v['date'];
                }
                
            }
            if(isset($v['date1'])){
                if($v['field'] == 'birthday'){
                    $data[$v['field']] .= ' '.$v['date1'];
                }else{
                    $data[$v['field']] .= '-'.$v['date1'];
                }

            }
        }
        $data['life_photo'] = $photo;
        $info = Db::table('ea_zhaopin')->where('id_card', $data['id_card'])->find();
        if(!empty($info)){
            
            $this->error('此身份证号已经由其他用户使用，请勿重复提交!如有疑问请联系招聘人');
        }
            $data['create_time'] =time();
            $data['create_month'] =date('m', time());
            $zid= Db::table('ea_zhaopin')->insertGetId($data);
    
            Db::table('ea_zhaopin_edu')->where('zid', $zid)->delete();
            $l = $list[21]['list'];
            foreach($l as $k=>$v){
                $edu = [];
                foreach($v['list'] as $kk=>$vv){
                    if(isset($vv['value'])){
                        $edu[$vv['field']] = $vv['value'];
                    }
                    if(isset($vv['date'])){
                        $edu[$vv['field']] = $vv['date'];
                        $edu['end'] = $vv['date1'];
                    }
                }
                $edu['type'] = 1;
                $edu['zid'] = $zid;
                $r = Db::table('ea_zhaopin_edu')->insertGetId($edu);
            }
            Db::table('ea_work_history')->where('zid', $zid)->delete();
            $ll = $list[25]['list'];
            foreach($ll as $kl=>$vl){
                if($kl == 0){
                    $q = $vl['list'];
                    $h = [];
                    foreach($q as $kx=>$vx){
    
                        if(isset($vx['value'])){
                            $h[$vx['field']] = $vx['value'];
                        }
                        if(isset($vx['date'])){
                            $h[$vx['field']] = $vx['date'];
                            $h['end'] = $vx['date1'];
                        }
                        
    
                    }
                    if(!empty($h)){
                        $h['zid'] = $zid;
                        $h['type'] = 1;
                        Db::table('ea_work_history')->insertGetId($h);
                    }
                }else{
                    $q2 = $vl['list'];
                    $h2 = [];
                    foreach($q2 as $kx2=>$vx2){
                        if(isset($vx2['value']) && $vx2['value'] != ''){
                            $h2[$vx2['field']] = $vx2['value'];
                        }
                        if(isset($vx2['date'])){
                            $h2[$vx2['field']] = $vx2['date'];
                            $h2['end'] = $vx2['date1'];
                        }
                        
                    }
                    //判断$h2是否为空
                    if(!empty($h2)){
                        $h2['zid'] = $zid;
                        $h2['type'] = 2;
                        Db::table('ea_work_history')->insertGetId($h2);
                    }
                    
                }
            }
            // 提交事务
            Db::commit();
            $this->success("保存成功");
        } catch (\think\db\exception\DbException $e) {
            // 回滚事务
            Db::rollback();
            $this->error("提交失败:" .$e->getMessage());
        }

    }

    public function save1(){
        Db::startTrans();
        try {
            $name = request()->param('name');
            $id_card = request()->param('id_card');
            $list = request()->param('list');
            $info = Db::table('ea_zhaopin')->where('id_card', $id_card)->find();
            if(empty($info)){
                $this->error('该用户不存在');
            }
            if($info['tjstatus'] == 1) {
                $this->error('不能重复提交！');
            }
            $data = [];
            foreach($list as $k=>$v){
                if(isset($v['value'])){
                    $data[$v['field']] = $v['value'];
                }
            }
    
            foreach($list[10]['list'] as $kk=>$vv){
                if(isset($vv['value'])){
                    $data[$vv['field']] = $vv['value'];
                }
            }
            foreach($list[11]['list'] as $kk=>$vvv){
                if(isset($vvv['value'])){
                    $data[$vvv['field']] = $vvv['value'];
                }
            }
            foreach($list[15]['list'] as $kk=>$vvv2){
                if(isset($vvv2['value'])){
                    $data[$vvv2['field']] = $vvv2['value'];
                }
            }
            foreach($list[30]['list'] as $kk=>$vvv3){
                if(isset($vvv3['value'])){
                    $data[$vvv3['field']] = $vvv3['value'];
                }
            }
            if($info['job'] == 7 || $info['job'] == 5) {
                $sz1 = [];
                $sz2 = [];
                $sz3 = [];
                $xz1 = [];
                $xz2 = [];
                $lt1 = [];
                foreach($list[39]['list'] as $kk=>$vvv4) {
                    if($vvv4['field'] == 'sz1'){
                        foreach($vvv4['list'] as $kk=>$vvv5){
                            if(isset($vvv5['date'])){
                                $sz1['date'] = $vvv5['date'];
                                $sz1['date1'] = $vvv5['date1'];
                            } else {
                                $sz1[$vvv5['field']] = $vvv5['value'];
                            }
                        }
                    }else if($vvv4['field'] == 'sz2'){
                        foreach($vvv4['list'] as $kk=>$vvv6){
                            if(isset($vvv6['date'])){
                                $sz2['date'] = $vvv6['date'];
                                $sz2['date1'] = $vvv6['date1'];
                            } else {
                                $sz2[$vvv6['field']] = $vvv6['value'];
                            }
                            
                        }
                    }else if($vvv4['field'] == 'sz3'){
                        foreach($vvv4['list'] as $kk=>$vvv7){
                            if(isset($vvv7['date'])){
                                $sz3['date'] = $vvv7['date'];
                                $sz3['date1'] = $vvv7['date1'];
                            } else {
                                $sz3[$vvv7['field']] = $vvv7['value'];
                            }
                        }
                    }else if($vvv4['field'] == 'xz1'){
                        foreach($vvv4['list'] as $kk=>$vvv8){
                        if(isset($vvv8['date'])){
                                $xz1['date'] = $vvv8['date'];
                                $xz1['date1'] = $vvv8['date1'];
                            } else {
                                $xz1[$vvv8['field']] = $vvv8['value'];
                            }
                        }
                    }else if($vvv4['field'] == 'xz2'){
                        foreach($vvv4['list'] as $kk=>$vvv9){
                            if(isset($vvv9['date'])){
                                $xz2['date'] = $vvv9['date'];
                                $xz2['date1'] = $vvv9['date1'];
                            } else {
                                $xz2[$vvv9['field']] = $vvv9['value'];
                            }
                        }
                    }else if($vvv4['field'] == 'lt1'){
                        foreach($vvv4['list'] as $kk=>$vvv10){
                            if(isset($vvv10['date'])){
                                $lt1['date'] = $vvv10['date'];
                                $lt1['date1'] = $vvv10['date1'];
                            } else {
                                $lt1[$vvv10['field']] = $vvv10['value'];
                            }
                        }
                    }
                }
                $data['sz1'] = json_encode($sz1);
                $data['sz2'] = json_encode($sz2);
                $data['sz3'] = json_encode($sz3);
                $data['xz1'] = json_encode($xz1);
                $data['xz2'] = json_encode($xz2);
                $data['lt1'] = json_encode($lt1);
            }
            
            $data['tjstatus'] = 1;
            $res = Db::table('ea_zhaopin')->where('id_card', $id_card)->update($data);
    
    
            if(!$res){
                return $this->error('提交失败，请稍后重试');
            }
            Db::table('ea_zhaopin_family')->where('zid', $info['id'])->delete();
            $lll = $list[40]['list'];
            foreach($lll as $kll=>$vll){
                $q = $vll['list'];
                $h = [];
                foreach($q as $kx=>$vx){
                    if(isset($vx['value'])){
                        $h[$vx['field']] = $vx['value'];
                    }
                    $h['zid'] = $info['id'];
                }
                Db::table('ea_zhaopin_family')->insertGetId($h);
    
            }
            Db::table('ea_zhaopin_contact')->where('zid', $info['id'])->delete();
            $ll = $list[41]['list'];
            foreach($ll as $kl=>$vl){
                    $q = $vl['list'];
                    $h = [];
                    foreach($q as $kx=>$vx){
                        if(isset($vx['value'])){
                            $h[$vx['field']] = $vx['value'];
                        }
                        $h['zid'] = $info['id'];
                    }
                    Db::table('ea_zhaopin_contact')->insertGetId($h);
    
            }
            Db::commit();
            $this->success("保存成功");
        }catch(\think\db\exception\DbException $e){
            Db::rollback();
            $this->error("提交失败,请联系招聘人！:" .$e->getMessage());
        }catch(\think\Exception $e){
            Db::rollback();
            $this->error("提交失败,请联系招聘人！" .$e->getMessage());
        }


    }
    public function getInfo(){
        $name = request()->param('name');
        $id_card = request()->param('id_card');
        $res = Db::table('ea_zhaopin')->where('name', $name)->where('id_card',$id_card)->find();
        if(!empty($res)){
            if($res['tjstatus'] == '1'){
                return $this->error('该用户已完成自评');
            }
            return $this->success('success',$res);
        }else{
            return $this->error('该用户不存在');
        }
    }
    public function link(){
        return $this->fetch();
    }

    public function index1(){
        return $this->fetch();
    }

    public function getList()
    {
        $md = request()->param('md');
        if(md5('zhaopinlist') != $md){
            return $this->error('认证失败');
        }

        $list = Db::table('ea_zhaopin')->where('lqstatus = 0 or (lqstatus = 1 and tjstatus = 1)')->select()->toArray();
        foreach($list as $k=>$v){
            $list[$k]['edu_json'] = Db::table('ea_zhaopin_edu')->where('zid','=',$v['id'])->select();
            $list[$k]['contact_json'] = Db::table('ea_zhaopin_contact')->where('zid','=',$v['id'])->select();
            $list[$k]['family_json'] = Db::table('ea_zhaopin_family')->where('zid','=',$v['id'])->select();
            $list[$k]['work_json'] = Db::table('ea_work_history')->where('zid','=',$v['id'])->select();
        }

        return $this->success('success',$list);
    }


    public function updateZp()
    {
        $ids = request()->param('ids');
        $zp = new ZhaopinModel();
        $zp->saveAll($ids);
    }


}
